<template>
	<view class="notice">
		<u-navbar :border-bottom="false" title="通知详情"></u-navbar>
		<view class="part01">
			<u-form :model="form" ref="uForm">
				<u-form-item class="formItem"><u-input v-model="form.title" :disabled="isDetail?'disabled':''" placeholder="请输入标题" /></u-form-item>
				<u-form-item class="formItem">
					<u-input v-model="form.content" :disabled="isDetail?'disabled':''" type="textarea" placeholder="请输入通知内容" :maxlength="99999"/>
				</u-form-item>
			</u-form>
		</view>
		<view class="part02 acea-row row-middle row-between" @click="showPop = true">
			<text>接收对象</text>
			<view>
				{{type=='add'?studentValue.studentId.length:defaultStudentValue.studentId.length}}人
				<u-icon name="arrow-right" color="#737981" size="32" style="margin-left: 24rpx;"></u-icon>
			</view>
		</view>
		<view :style="'height: calc(152rpx + '+iosSafeBottom+'px + '+SafeBottom+'rpx);'"></view>
		<view class="footer" v-if="!isDetail" :style="'padding-bottom: calc('+iosSafeBottom+'px + '+SafeBottom+'rpx);box-sizing: content-box;'">
			<view class="btn" @click="submit">发布通知</view>
		</view>
		<u-popup mode="center" v-model="showPop" width="100%" height="100%" >
			<studentSelect ref="studentSelect" :value.sync="studentValue" :defaultValue="defaultStudentValue" :show.sync="showPop"></studentSelect>
		</u-popup>
	</view>
</template>

<script>
	import request from '../../../api/request.js'
	import common from '../../../api/common.js'
	const app = getApp()
	export default{
		data(){
			return{
				type:'add',
				isDetail:false,
				iosSafeBottom:app.globalData.iosSafeBottom,
				SafeBottom:app.globalData.iosSafeBottom==0?'24':'-24',
				form:{
					title:'',
					content:''
				},
				studentValue:{
					studentId:[]
				},
				defaultStudentValue:{
					studentId:[]
				},
				showPop:false
			}
		},
		onLoad(e) {
			if(e.id){
				this.getInfo(e.id)
				this.isDetail = true
			}else{
				this.isDetail = false
			}
			this.type = e.type
		},
		methods:{
			submit(){
				if(this.checkForm()){
					let data = {
						...this.form,
						student:this.studentValue.studentId.toString()
					}
					request.go((e)=>{
						uni.showToast({
							title:"发布成功",
							icon:"success",
							duration:600
						})
						setTimeout(()=>{
							uni.navigateBack({})
						},600)
					},"/tongzhi/xttz_add",data,"POST")
				}
			},
			checkForm(){
				if(this.form.title == ''){
					uni.showToast({
						icon:"none",
						title:"请输入标题"
					})
					return false;
				}else if(this.form.content == ''){
					uni.showToast({
						icon:"none",
						title:"请输入内容"
					})
					return false;
				}else if(this.studentValue.studentId.length == 0){
					uni.showToast({
						icon:"none",
						title:"请选择学员"
					})
					return false;
				}else{
					return true;
				}
			},
			getInfo(id){
				let data = {
					id
				}
				request.go((e)=>{
					let form = {
						title:e.data.title,
						content:e.data.content
					}
					this.form = form
					let studentId = []
					for(let i of e.data.student){
						let obj = {
							id:i.id,
							state:i.state
						}
						studentId.push(obj)
					}
					this.defaultStudentValue.studentId = studentId
				},"/tongzhi/xttz_view",data,"POST")
			}
		}
	}
</script>

<style scoped>
	.notice{
		min-height: 100vh;
		background-color: #F7F8FA;
	}
	.notice .part01{
		width: 686rpx;
		background-color: #FFFFFF;
		padding: 32rpx;
		box-sizing: border-box;
		border-radius: 24rpx;
		margin: 32rpx auto 0;
	}
	.notice .part02{
		width: 686rpx;
		margin: 32rpx auto 0;
		padding: 26rpx 32rpx;
		background-color: #FFFFFF;
		border-radius: 24rpx;
	}
	.notice .part02{
		font-size: 32rpx;
		font-family: PingFang SC, PingFang SC-Regular;
		font-weight: 400;
		color: #1f2329;
		line-height: 44rpx;
	}
	.notice .footer{
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 120rpx;
		background-color: #FFFFFF;
		z-index: 999;
	}
	.notice .footer .btn{
		width: 622rpx;
		height: 88rpx;
		line-height: 88rpx;
		text-align: center;
		margin: 24rpx auto 0;
		font-size: 32rpx;
		font-family: PingFang SC, PingFang SC-Regular;
		font-weight: 400;
		color: #ffffff;
		z-index: 99;
		background-color: #1661FF;
	}
</style>
